home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / jx4nt123.zip / UTILS / SYSCALLS.UTF (.txt) < prev    next >
Null Bytes Alternating  |  1994-08-21  |  13KB  |  226 lines

  1. \ syscalls.utf .. calling DLLs from Jax4th
  2. \ Copyright (c)1994 Jack J. Woehr
  3. \ P.O. Box 51, Golden, Colorado 80402-0051
  4. \ jax@well.sf.ca.us 72203.1320@compuserve.com
  5. \ SYSOP RCFB (303) 278-0364 3/12/24/96/14.4 24 hours
  6. \ All Rights Reserved
  7. \ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. \ This is free software and can be modified and redistributed under
  9. \ certain conditions described in the file COPYING.TXT. The
  10. \ Disclaimer of Warranty and License for this free software are also
  11. \ contained in the file COPYING.TXT.
  12. \ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  13.  
  14. \ $Revision: 1.2 $
  15.  
  16. MARKER syscalls.utf
  17.  
  18. \ ~~~~~~~~~~~~~~~~~~~~
  19. \ Conditional INCLUDED
  20. \ ~~~~~~~~~~~~~~~~~~~~
  21.  
  22. : PROVIDES ( c-addr u "ccc< >" --)
  23.     BL WORD FIND NIP 0=
  24.     IF INCLUDED ELSE 2DROP THEN ;
  25.  
  26. S" UTILS\UTILS.UTF" PROVIDES USEFUL
  27.  
  28. CR .( Loading System Call Examples) CR
  29.  
  30. \ ~~~~~~~~~~~~~~~~~~~~~~~~~~
  31. \ Libraries and System Calls
  32. \ ~~~~~~~~~~~~~~~~~~~~~~~~~~
  33.  
  34. DECIMAL USEFUL
  35. INTERNALS-WORDLIST ALSO-WID DEFINITIONS
  36.  
  37. \ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  38. \ Adds a system call compilation regimen. The user may open
  39. \ all referenced libraries and resolve all referenced system
  40. \ calls at once. Greate for boot words in saved images.
  41. \ Dependecies on NT operating system.
  42. \ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  43.  
  44. \ Backlink pointer for list of libs to open.
  45. VARIABLE LIBRARIES-LINK
  46. : LLINIT ( --) 0 LIBRARIES-LINK ! ; LLINIT
  47.  
  48. \ Backlink pointer for list of syscalls to resolve.
  49. VARIABLE SYSCALLS-LINK
  50. : SLINIT ( --) 0 SYSCALLS-LINK ! ; SLINIT
  51.  
  52. \ Linked list handling for these def-to-def pointers.
  53. : BACKLINK  ( var n  --)
  54.     DP @ SWAP
  55.     CELLS - MAKETOKEN
  56.     OVER @ ,
  57.     SWAP !
  58. ;
  59. \ Link library in to list of libraries
  60. \   n is number of cells to back up to find execution addr.
  61. \   var is the address of the end-of-list pointer.
  62. \   Compile backlink, store new link.
  63.  
  64. USEFUL INTERNALS-WORDLIST ALSO-WID
  65.  
  66. \ Declare a dynamic link library.
  67. \ This works with any NT DLL, not just NT's own
  68. : LIBRARY ( c-addr u --)
  69.     CREATE 0 , LIBRARIES-LINK 2 BACKLINK
  70.     TUCK HERE SWAP CMOVE
  71.     CHARS ALLOT 0 C, ALIGN
  72. ;
  73. \ Data structure of library: /lib-ptr/library-link/string...
  74. \ lib-ptr is the storage slot for the library handle
  75. \ library-link is the backlink to the previous declared lib
  76.  
  77. \ Open a DLL and store its handle
  78. : OPEN-LIB ( a-addr --)
  79.     DUP 2 CELLS + DATATOABS \ find string in body of Library construct
  80.     0 0 ROT LoadLibraryEx   \ call system
  81.     ?DUP 0=                 \ failure?
  82.     ABORT" Couldn't open library."
  83.     SWAP !                  \ done, save handle
  84. ;
  85.  
  86.     
  87. \ We keep libraries in a linked list.
  88. \ Open all declared libraries
  89. : OPEN-ALL ( --)
  90.     LIBRARIES-LINK @    \ Variable holding pointer to last lib
  91.     BEGIN
  92.         ?DUP            \ is it a pointer or null?
  93.     WHILE               \ while it's a pointer
  94.         EXECUTE         \ (actually xt) execute it
  95.         DUP OPEN-LIB
  96.      CELL+              \ then convert xt to data address
  97.      @                  \ and find address of backptr
  98.     REPEAT              \ loop until libs all opened
  99. ;
  100.  
  101. \ And close all, good practice!
  102. \ Walk thru linked list of libraries and close
  103. : CLOSE-ALL ( --)
  104.     LIBRARIES-LINK @
  105.     BEGIN ?DUP
  106.     WHILE EXECUTE DUP FreeLibrary DROP
  107.      CELL+ @
  108.     REPEAT
  109. ;
  110.  
  111. \ Declaring a DLL call, also kept in a linked list.
  112. \ This works with any NT DLL, not just NT's own.
  113. : SYSTEMCALL ( c-addr u a-addr --)
  114.     CREATE 0 , , SYSCALLS-LINK 2 BACKLINK
  115.     DUP C,
  116.     TUCK HERE SWAP CMOVE
  117.     1+ CHARS ALLOT ALIGN
  118.     DOES> ( i*x -- j*x)
  119.     @ SYSCALL
  120. ;
  121. \ The data structure of a system call:
  122. \ /calladdr/lib/backptr string ...
  123. \ calladdr stores the resolved addr after this call resolved
  124. \ lib is the address returned by a LIBRARY construct
  125. \ string is the case-sensitive name of call (e.g., Beep)
  126. \ backptr is analogous to the library back pointer: all calls
  127. \ are linked back so that all can be resolved at once.
  128.  
  129. SYSTEM-WORDLIST SET-CURRENT
  130.  
  131. \ Given the data address of the data structure of a system call,
  132. \ resolve that call. Presumes library open already.
  133. : (RESOLVE-CALL) ( a-addr --)
  134.      DUP CELL+ @ @          \ Library pointer
  135.      OVER 3 CELLS +         \ Call name string)
  136.      COUNT ASCIIZ DATATOABS \ Address of null-terminated ASCII)
  137.      SWAP GetProcAddress
  138.      ?DUP 0= ABORT" Couldn't resolve call."
  139.      SWAP !
  140. ;
  141.  
  142. USEFUL
  143.  
  144. \ Wrapper of above.
  145. : RESOLVE-CALL ( "ccc" --)
  146.     ' >BODY
  147.     STATE @
  148.     IF   POSTPONE LITERAL POSTPONE (RESOLVE-CALL)
  149.     ELSE (RESOLVE-CALL)
  150.     THEN
  151. ; IMMEDIATE
  152.  
  153. INTERNALS-WORDLIST ALSO-WID
  154.  
  155. \ Resolve all currently defined calls. Presumes appropriate
  156. \ libs are open. Harmless to do twice or three times.
  157. : RESOLVE-ALL ( --)
  158.     SYSCALLS-LINK @
  159.     BEGIN
  160.         ?DUP
  161.     WHILE
  162.         >BODY DUP (RESOLVE-CALL)
  163.         2 CELLS + @
  164.     REPEAT
  165. ;
  166.  
  167. USEFUL
  168.  
  169. \ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  170. \ Some Libraries and Calls Declared
  171. \ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  172.  
  173. \ Let's declare a few DLLs, to be resolved.
  174. S" KERNEL32.DLL" LIBRARY KERNEL32
  175. S" GDI32.DLL" LIBRARY GDI32
  176. S" USER32.DLL" LIBRARY USER32
  177.  
  178. \ Let's declare a few calls, to be resolved
  179. S" Beep" KERNEL32 SYSTEMCALL BEEP
  180. S" SetCurrentDirectoryA"  KERNEL32 SYSTEMCALL (CHDIR)
  181. S" GetCurrentDirectoryW"  KERNEL32 SYSTEMCALL (GTDIR)
  182.  
  183. \ Here's a high-level word to illustrate system calls.
  184. : CHDIR ( "<>chars<>" --)
  185.     BL WORD COUNT DUP 0<>           \ did user provide a path
  186.     IF                              \ if so, change path
  187.         ASCIIZ DATATOABS (CHDIR)
  188.     THEN
  189.     2DROP                           \ we don't care about returns here
  190.     CR PAD DATATOABS 128 (GTDIR)    \ get the current parth
  191.     2DROP                           \ we don't care about returns here
  192.     PAD 128 0TYPE                   \ show the current path
  193. ;
  194.  
  195. \ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  196. \ Demo: Mary Had A Little Lamb
  197. \ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  198.  
  199. SYSTEM-WORDLIST NONSTANDARD-WORDLIST FORTH-WORDLIST
  200. 3 SET-ORDER DEFINITIONS
  201.  
  202. DECIMAL
  203.  
  204. : MHALL ( --)
  205.     CR ." Now I'm going to sing! ..."
  206.     CR ." Ma-"   500 657 BEEP ." ry "  500 586 BEEP
  207.        ." had "  500 522 BEEP ." a "   500 586 BEEP
  208.        ." lit-"  500 657 BEEP ." tle " 500 657 BEEP
  209.        ." lamb." 500 657 BEEP
  210.     7 0 DO 2DROP LOOP
  211.     CR
  212. ;
  213.  
  214. OPEN-ALL RESOLVE-ALL
  215. MHALL
  216. CLOSE-ALL
  217.  
  218. USEFUL DECIMAL
  219.  
  220. \ ~~~~~~~~~~~~~~~~~
  221. \ End of syscalls.h
  222. \ ~~~~~~~~~~~~~~~~~
  223.  
  224.